﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PropertyManagementSystemAPI.Models;
using static FreeSql.Internal.GlobalFilter;

namespace PropertyManagementSystemAPI.Controllers.PropertyManage
{
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "保修接口")]
    public class WarrantyController : ControllerBase
    {

        public readonly IFreeSql db;
        public WarrantyController(IFreeSql _db)
        {
            db = _db;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="ys">每页显示的条目数</param>
        /// <param name="ym">当前页码</param>
        /// <param name="name">保修名称</param>
        /// <returns></returns>
        [HttpGet]
        public Object Query(int ys, int ym, string? name)
        {

         
            int pageSize = ys; // 每页显示的条目数
            int pageIndex = ym; // 当前页码
            var query = db.Select< Warranty>().ToList();
            int totalCount = query.Count(); // 总条目数
            var list = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

            // 获取表中所有列的数据
            var data = list.Select(item => new
            {
                item.WId,
                item.WCellId,
                item.WUserId,
                item.WEquipment,
                item.WText,
                item.WCreateTime,
                item.WState,
                item.WEndTime,
                item.WApplication,
                item.WAssess,
                item.WRemark
            }).ToList();

            return new
            {
                TotalCount = totalCount,
                Data = data
            };
            


        }
        [HttpPost]
        public int AddUser(Warranty user)
        {
            // 添加学生信息数据
            var studentInfos = new Warranty()
            {
                WId = user.WId,
                WCellId = user.WCellId,
                WUserId = user.WUserId,
                WEquipment = user.WEquipment,
                WText = user.WText,
                WCreateTime = user.WCreateTime,
                WState = user.WState,
                WEndTime = user.WEndTime,
                WApplication=user.WApplication,
                WAssess=user.WAssess,
                WRemark=user.WRemark
            };
            var q = db.Insert(studentInfos).ExecuteAffrows();
            return q;
             
        }

        [HttpPut("{id}")]
       
            public int Put(int id, Warranty user)
            {
                var mm = db.Update<Warranty>().Set(a => new Warranty
                {
                    WId = user.WId,
                    WCellId = user.WCellId,
                    WUserId = user.WUserId,
                    WEquipment = user.WEquipment,
                    WText = user.WText,
                    WCreateTime = user.WCreateTime,
                    WState = user.WState,
                    WEndTime = user.WEndTime,
                    WApplication = user.WApplication,
                    WAssess = user.WAssess,
                    WRemark = user.WRemark
                }).Where(a => a.WId == id).ExecuteAffrows();
                return mm;
            }
        

        [HttpDelete("{id}")]
        public IActionResult DeleteUser(int id)
        {
            db.Delete<Warranty>().Where(u => u.WId == id).ExecuteAffrows();
            return Ok();
        }
    }
}
